<script setup lang="js">
import { onLaunch, onShow, onHide, onUnload } from '@dcloudio/uni-app'
import 'abortcontroller-polyfill/dist/abortcontroller-polyfill-only'
import { useSysStore } from '@/store/systemInfo'
import { userLogin, getUserInfo } from '@/api/user'
import dd from 'dingtalk-jsapi' // 引入钉钉api

const sysStore = useSysStore()

onLaunch(() => {
  // console.log('App Launch')
})
onShow(() => {
  initFnc()
  // console.log('App Show')
})
onHide(() => {
  // console.log('App Hide')
})
onUnload(() => {
  // console.log('App onUnload')
  sysStore.setUserInfo({})
})

// 初始化
const initFnc = () => {
  // 钉钉免登
  const corpId = sysStore.corpId
  dd.ready(() => {
    dd.runtime.permission.requestAuthCode({
      corpId: corpId,
      onSuccess: async function (info) {
        const res = await userLogin({ code: info.authCode || info.code })
        sysStore.setToken(res.data.data.token)
        const res2 = await getUserInfo()
        sysStore.setUserInfo({ ...res2.data.user } || {})
        sysStore.setRoles(res2.data.roles || [])
        // roles.value = res2.data.roles || []
      },
      onFail: function (err) {
        console.log(err, 888)
      },
    })
  })
}

const updateDD = () => {
  const updateManager = dd.getUpdateManager()

  updateManager.onCheckForUpdate(function (res) {
    // 请求完新版本信息的回调
    console.log(res.hasUpdate) // 是否有更新
  })

  updateManager.onUpdateReady(function (ret) {
    console.log(ret.version) // 更新版本号
    dd.confirm({
      title: '更新提示',
      content: '新版本已经准备好，是否重启应用？',
      success: function (res) {
        if (res.confirm) {
          // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
          updateManager.applyUpdate()
        }
      },
    })
  })
  updateManager.onUpdateFailed(function () {
    // 新版本下载失败
  })
}
</script>

<style lang="scss">
/* stylelint-disable selector-type-no-unknown */
button::after {
  border: none;
}

swiper,
scroll-view {
  flex: 1;
  height: 100%;
  overflow: hidden;
}

image {
  width: 100%;
  height: 100%;
  vertical-align: middle;
}

// 单行省略，优先使用 unocss: text-ellipsis
.ellipsis {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

// 两行省略
.ellipsis-2 {
  display: -webkit-box;
  overflow: hidden;
  text-overflow: ellipsis;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

// 三行省略
.ellipsis-3 {
  display: -webkit-box;
  overflow: hidden;
  text-overflow: ellipsis;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}
</style>
